package com.tlkj.common.curd.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * @author flyong86
 * @since 2024年6月19日
 */
@Slf4j
@org.springframework.context.annotation.Configuration
public class CommonCurdMyBatisConfig {

    public final static String COMMON_CURD_MAPPER_LOCATION = "classpath:/mapper/common-curd/*.xml";

    @Bean
    @ConditionalOnMissingBean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources(COMMON_CURD_MAPPER_LOCATION)
        );
        return sessionFactory.getObject();
    }

}
